
import { httpRequest } from './httpRequest.js'    //引入类

export default class user{
  constructor(options){

  }
  static login(phone){
    let that = this;
    return new Promise((resolve, reject) => {
      let data = `grant_type=password&username=wx.${phone}&password=123456`;
      //load desc
      httpRequest.sendRrquest(`${getApp().globalData.baseUrl}/token`, "post", data)
        .then(
          res => {
            console.log("login", res);
            getApp().globalData.access_token = res.data.access_token;
            getApp().globalData.openid = res.data.openid;
            wx.setStorageSync("access_token", getApp().globalData.access_token)
            wx.setStorageSync("openid", getApp().globalData.openid)
            resolve(res);
          }
        ).catch(() => {
          reject();
        })
    })
  }
  static modifyOpenid(openid) {
    return new Promise((resolve, reject) => {
      getApp().globalData.api.http_get(`${getApp().globalData.baseUrl}/api/sys/user/openid?openid=` + openid)
        .then(
          res => {
            resolve(res);
          }
        )
    })
  }
  static load_my() {
      let that = this;
      return new Promise((resolve, reject) => {
        httpRequest.sendRrquest(`${getApp().globalData.baseUrl}/api/sys/user/my`, "get", null,
          {
            'content-type': 'application/x-www-form-urlencoded',
            'Authorization': 'Bearer ' + getApp().globalData.access_token,
            "openid": getApp().globalData.openid
          })
          .then(
            res => {
              let _user =res.data.data;
              getApp().globalData.user = _user;
              wx.setStorageSync("user", getApp().globalData.user)
              resolve(_user);
            }
          )
      })
    }
}